package Util;

import Actor.Actor;
import Actor.Actor.DownLeftCommand;
import Actor.Actor.DownRightCommand;
import Actor.Actor.UpLeftCommand;
import Actor.Actor.UpRightCommand;
import Actor.IfaceCharAction;
import Actor.Movable;
import Main.Constant;
import Map.Floor;
import Map.EnumTile;
import Map.Tile;
import View.ViewManager;

public class CollisionUtil {
	public static boolean doCollisionCheck(Actor actor, Actor monster) {
		// int r1x1 = actor.getX();
		// int r1y1 = actor.getY();
		// int r1x2 = actor.getX() + EnumTile.height;
		// int r1y2 = actor.getY() + EnumTile.height;
		// // monster
		// int r2x1 = monster.getX();
		// int r2y1 = monster.getY();
		// int r2x2 = monster.getX() + EnumTile.height;
		// int r2y2 = monster.getY() + EnumTile.height;

		int r1CX = actor.getX() * Constant.tileWidth + Constant.tileWidth / 2;
		int r1CY = actor.getY() * Constant.tileHeight + Constant.tileHeight / 2;

		int r2CX = monster.getX() * Constant.tileWidth + Constant.tileWidth / 2;
		int r2CY = monster.getY() * Constant.tileHeight + Constant.tileHeight
				/ 2;
		double distance = Math
				.pow(((r1CX - r2CX) * (r1CX - r2CX) + (r1CY - r2CY)
						* (r1CY - r2CY)) * 1.0d, 0.5);
		return distance < Constant.tileWidth * 2;
	}

	public static int getStance(Actor actor, Actor monster) {
		// int r1x1 = actor.getX();
		// int r1y1 = actor.getY();
		// int r1x2 = actor.getX() + EnumTile.height;
		// int r1y2 = actor.getY() + EnumTile.height;
		// // monster
		// int r2x1 = monster.getX();
		// int r2y1 = monster.getY();
		// int r2x2 = monster.getX() + EnumTile.height;
		// int r2y2 = monster.getY() + EnumTile.height;

		int r1CX = actor.getX() * Constant.tileWidth + Constant.tileWidth / 2;
		int r1CY = actor.getY() * Constant.tileHeight + Constant.tileHeight / 2;

		int r2CX = monster.getX() * Constant.tileWidth + Constant.tileWidth / 2;
		int r2CY = monster.getY() * Constant.tileHeight + Constant.tileHeight
				/ 2;
		double distance = Math
				.pow(((r1CX - r2CX) * (r1CX - r2CX) + (r1CY - r2CY)
						* (r1CY - r2CY)) * 1.0d, 0.5);
		return (int) (distance / Constant.tileHeight);
	}

	// public static boolean doCollisionCheck(Actor actor, Tile[][] map) {
	// for (int i = (actor.getX()); i < (actor.getX()) + 2; i++) {
	// for (int j = (actor.getY()); j < (actor.getY()) + 2; j++) {
	// try {
	// if (map[i][j].getType() == EnumTile.BOUNDARY)
	// return false;
	// } catch (Exception e) {
	//
	// return true;
	// }
	// }
	// }
	// return true;
	//
	// }

	// public static boolean doTryCollisionCheck(Movable actor, Tile[][] map) {
	// for (int i = (actor.getX()); i < (actor.getX()) + 2; i++) {
	// for (int j = (actor.getY()); j < (actor.getY()) + 2; j++) {
	// if (i < 0 && j < 0) {
	// continue;
	// }
	// EnumTile tile = map[i*10][j*10].getType();
	// try {
	// if (tile == EnumTile.NORMAL)
	// return true;
	// } catch (Exception e) {
	// return false;
	// }
	// // }
	// }
	// }
	// return false;
	//
	// }
	public static boolean doTryCollisionCheck(Movable actor, Floor map1,IfaceCharAction action,ViewManager viewManager) {
		int leftTile = actor.getX() / 10;      // bounds = Rectangle of your entity
		int topTile = actor.getY() / 10;
		int rightTile = (int)Math.ceil((float)actor.getX() / 10);
		int bottomTile = (int)Math.ceil(((float)actor.getY() / 10)) ;
		int viewY = viewManager.getTesty()/10;
		int viewX = viewManager.getTestx()/10;
		Tile[][] map = map1.getMap(viewY, viewX, 50, 50);
//		for (int i = 0; i < Constant.VIEWSIZE; i++) {
//		for (int j = 0; j < Constant.VIEWSIZE; j++) {
//			if(topTile-viewY==i&&leftTile- viewX==j)
//				System.out.print("*");
//			else{
//				if(map[i][j].getType()==EnumTile.NORMAL)
//					System.out.print("0");
//				else
//					System.out.print(".");
//			}	
//		}
//		System.out.println();
//	}
//	System.out.println("-------------------------------------------------------");
		if(action instanceof DownRightCommand){// down			
			try {
				EnumTile tile = map[bottomTile-viewY][rightTile- viewX].getType();
				if (tile == EnumTile.NORMAL)
					return true;
			} catch (Exception e) {
				return false;
			}	
		}else if(action instanceof DownLeftCommand){// left
			
			try {
				EnumTile tile = map[bottomTile-viewY][leftTile- viewX].getType();
				if (tile == EnumTile.NORMAL)
					return true;
			} catch (Exception e) {
				return false;
			}	
		}else if(action instanceof UpLeftCommand){// up
			
			try {
				EnumTile tile = map[topTile-viewY][leftTile- viewX].getType();
				if (tile == EnumTile.NORMAL)
					return true;
			} catch (Exception e) {
				return false;
			}	
		}else if(action instanceof UpRightCommand){// right
			
			try {
				EnumTile tile = map[topTile-viewY][rightTile-viewX].getType();
				if (tile == EnumTile.NORMAL)
					return true;
			} catch (Exception e) {
				return false;
			}	
		}else{
			return false;
		}
		
		return false;
	}
//	public static boolean doTryCollisionCheck(Movable actor, Continent map1,IfaceCharAction action,ViewManager viewManager,Object n) {
//		int leftTile = actor.getX() / 10;      // bounds = Rectangle of your entity
//		int topTile = actor.getY() / 10;
//		int rightTile = (int)Math.ceil((float)actor.getX() / 10);
//		int bottomTile = (int)Math.ceil(((float)actor.getY() / 10)) ;
//		Tile[][] map = map1.getMap(viewManager.getViewY(), viewManager.getViewX(), 50, 50);
////		for (int i = 0; i < Constant.VIEWSIZE; i++) {
////			for (int j = 0; j < Constant.VIEWSIZE; j++) {
////				if(topTile-viewManager.getViewY()==i&&leftTile- viewManager.getViewX()==j)
////					System.out.print("*");
////				else{
////					if(map[i][j].getType()==EnumTile.NORMAL)
////						System.out.print("0");
////					else
////						System.out.print(".");
////				}	
////			}
////			System.out.println();
////		}
////		System.out.println("-------------------------------------------------------");
//		if(action instanceof DownRightCommand){// down			
//			try {
//				EnumTile tile = map[bottomTile-viewManager.getViewY()][rightTile- viewManager.getViewX()].getType();
//				if (tile == EnumTile.NORMAL)
//					return true;
//			} catch (Exception e) {
//				return false;
//			}	
//		}else if(action instanceof DownLeftCommand){// left
//			
//			try {
//				EnumTile tile = map[bottomTile-viewManager.getViewY()][leftTile- viewManager.getViewX()].getType();
//				if (tile == EnumTile.NORMAL)
//					return true;
//			} catch (Exception e) {
//				return false;
//			}	
//		}else if(action instanceof UpLeftCommand){// up
//			
//			try {
//				EnumTile tile = map[topTile-viewManager.getViewY()][leftTile- viewManager.getViewX()].getType();
//				if (tile == EnumTile.NORMAL)
//					return true;
//			} catch (Exception e) {
//				return false;
//			}	
//		}else if(action instanceof UpRightCommand){// right
//			
//			try {
//				EnumTile tile = map[topTile-viewManager.getViewY()][rightTile- viewManager.getViewX()].getType();
//				if (tile == EnumTile.NORMAL)
//					return true;
//			} catch (Exception e) {
//				return false;
//			}	
//		}else{
//			return false;
//		}
//		
//		return false;
//	}
}
